package cn.easybuy.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import cn.easybuy.dao.impl.UserDaoImpl;
import cn.easybuy.entity.User;
import cn.easybuy.service.UserService;
import cn.easybuy.utils.DataSourceUtil;

public class UserServiceImpl implements UserService {

	@Override
	public User loginUser(String userName, String pwd) throws Exception {
		Connection conn = DataSourceUtil.getConnection();
		try {
			User user = new UserDaoImpl(conn).loginUser(userName, pwd);
			return user;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DataSourceUtil.closeAll(conn, null, null);
		}

	}

	@Override
	public int loginNameNum(String loginName) throws Exception {
		Connection conn = DataSourceUtil.getConnection();
		try {
			int result = new UserDaoImpl(conn).loginNameNum(loginName);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DataSourceUtil.closeAll(conn, null, null);
		}
	}

	@Override
	public int registerUser(User userutil) throws Exception {
		Connection conn = DataSourceUtil.getConnection();
		try {
			int result = new UserDaoImpl(conn).registerUser(userutil);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DataSourceUtil.closeAll(conn, null, null);
		}
	}

	@Override
	public List<User> selectAll() throws Exception {
		Connection conn = DataSourceUtil.getConnection();
		try {
			List<User> userList = new UserDaoImpl(conn).selectAll();
			return userList;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DataSourceUtil.closeAll(conn, null, null);
		}
	}

	@Override
	public List<User> selectUser(int id) throws Exception {
		Connection conn = DataSourceUtil.getConnection();
		try {
			List<User> userList = new UserDaoImpl(conn).selectUser(id);
			return userList;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DataSourceUtil.closeAll(conn, null, null);
		}
	}

	@Override
	public List<User> selectUser(String name) throws Exception {
		Connection conn = DataSourceUtil.getConnection();
		try {
			List<User> userList = new UserDaoImpl(conn).selectUser(name);
			return userList;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DataSourceUtil.closeAll(conn, null, null);
		}
	}

	@Override
	public List<User> selectUserInfo(String name) throws Exception {
		Connection conn = DataSourceUtil.getConnection();
		try {
			List<User> userList = new UserDaoImpl(conn).selectUserInfo(name);
			return userList;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DataSourceUtil.closeAll(conn, null, null);
		}
	}

	@Override
	public List<User> selectUserInfo2(String name) throws Exception {
		Connection conn = DataSourceUtil.getConnection();
		try {
			List<User> userList = new UserDaoImpl(conn).selectUserInfo2(name);
			return userList;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DataSourceUtil.closeAll(conn, null, null);
		}
	}

	@Override
	public int delUser(int id) throws Exception {
		Connection conn = DataSourceUtil.getConnection();
		try {
			int result = new UserDaoImpl(conn).delUser(id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DataSourceUtil.closeAll(conn, null, null);
		}

	}

	@Override
	public int addUser(User user) throws Exception {
		Connection conn = DataSourceUtil.getConnection();
		try {
			int result = new UserDaoImpl(conn).addUser(user);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DataSourceUtil.closeAll(conn, null, null);
		}
	}

	@Override
	public int updateUser(User user) throws Exception {
		Connection conn = DataSourceUtil.getConnection();
		try {
			int result = new UserDaoImpl(conn).updateUser(user);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DataSourceUtil.closeAll(conn, null, null);
		}
	}

	@Override
	public int updatePwd(int id, String password) throws Exception {
		Connection conn = DataSourceUtil.getConnection();
		try {
			int result = new UserDaoImpl(conn).updatePwd(id, password);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DataSourceUtil.closeAll(conn, null, null);
		}
	}
}
